local VERSION = "v1.04"
local Sol = LibStub("Sol")

CopyChat = {}

CopyChat.DefaultSettings = {
    MaxMessages = 1000,
    Refresh = false,
}

CopyChat.Messages = {}

local NUM_DISPLAYED_MESSAGES = 10

local selectedTab = nil

function CopyChat.OnLoad(frame)
    frame:RegisterEvent("VARIABLES_LOADED")
end

function CopyChat.OnEvent(frame, _event, _arg1)
	if ( _event == "VARIABLES_LOADED" ) then
    
        Sol.config.CheckSettings("CopyChat", CopyChat.DefaultSettings)
        Sol.config.CreateSlashToHandleConfig("CopyChat", CopyChatFrame, "cc")
        
        CopyChat.Enable()
        
        for i = 1, NUM_CHAT_WINDOWS do
            CopyChat.Messages[i] = {}
        end
        
        selectedTab = CopyChatFrameTabsTab1
    end
end


function CopyChat.Enable()
    for i = 1, NUM_CHAT_WINDOWS do
        Sol.hooks.Hook("CopyChat", "AddMessage", CopyChat.AddMessage, _G["ChatFrame" .. i])
    end
end

function CopyChat.Disable()
    for i = 1, NUM_CHAT_WINDOWS do
        Sol.hooks.UnHook("CopyChat", "AddMessage", _G["ChatFrame" .. i])
    end
end

function CopyChat.AddMessage(frame, msg, r, g, b)
    Sol.hooks.GetOriginalFn("CopyChat", "AddMessage", frame)(frame, msg, r, g, b)
    
    local messagesTable = CopyChat.Messages[frame:GetID()]
    table.insert(messagesTable, msg)
    while CopyChat_Settings.MaxMessages > 0 and #messagesTable > CopyChat_Settings.MaxMessages do
        table.remove(messagesTable, 1)
    end
    
    if CopyChatFrame:IsVisible() and CopyChat_Settings.Refresh then
        CopyChat.OnTabChanged(selectedTab)
    end
end

function CopyChat.OnShow(frame)
    for i = 1, 8 do 
        _G["CopyChatFrameTabsTab" .. i]:SetText(_G["ChatFrame" .. i .. "TabText"]:GetText())
    end
    CopyChat.OnTabChanged(selectedTab)
end

function CopyChat.OnTabChanged(tab)
    if selectedTab then
        selectedTab:Enable()
    end
    tab:Disable()
    selectedTab = tab
    
    CopyChat.Update()
    CopyChatScrollBar:SetValue(CopyChatScrollBar:GetMaxValue())
end

function CopyChat.Update()
    local searchStr = CopyChatFrameSearchText:GetText() or ""
    local value = CopyChatScrollBar:GetValue()
    local messagesTable = CopyChat.Messages[selectedTab:GetID()]
    
    for i = 1, NUM_DISPLAYED_MESSAGES do
        local editbox = _G["CopyChatMessage" .. i]
        editbox:Hide()
    end
    
    local count = 0
    local editBoxIndex = 1
    for i = 1, #messagesTable do
        local msg = messagesTable[i]
        
        if msg and (searchStr == "" or msg:find(searchStr)) then
            if (editBoxIndex <= NUM_DISPLAYED_MESSAGES and count >= value) then
                local editbox = _G["CopyChatMessage" .. editBoxIndex]
                editbox:Show()
                editbox:SetText(msg)
                editBoxIndex = editBoxIndex + 1
            end
            count = count + 1
        end
    end
    CopyChat.UpdateScrollBarMax(count)
end

function CopyChat.UpdateScrollBarMax(numMessages)
    if numMessages <= NUM_DISPLAYED_MESSAGES then
        CopyChatScrollBar:Hide()
        CopyChatScrollBar:SetValue(0)
        CopyChatScrollBar:SetMaxValue(0)
    elseif numMessages ~= CopyChatScrollBar:GetMaxValue() then
        CopyChatScrollBar:Show()
        CopyChatScrollBar:SetMinMaxValues(0, numMessages - NUM_DISPLAYED_MESSAGES)
    end
end

function CopyChat.Search()
    CopyChat.Update()
end
